#!/bin/bash -e
port=$1
[ -z "$port" ] && port=5300
limit=$2
[ -z "$limit" ] && limit=100000
threads=$3
[ -z "$threads" ] && threads=8
mthreads=$4
[ -z "$mthreads" ] && mthreads=2048
shards=$5
[ -z "$shards" ] && shards=1024

: ${RECURSOR:="../pdns/recursordist/pdns_recursor"}
: ${RECCONTROL:="../pdns/recursordist/rec_control"}
: ${CSV:="top-1m.csv"}
: ${IPv6:="0"}
: ${TRACE:="fail"}
: ${DNSBULKTEST:="../pdns/dnsbulktest"}

echo Current working dir is `pwd`

if [ $IPv6 = 1 ]
then
    echo \$ dig -6 @k.root-servers.net . SOA
    dig -6 @k.root-servers.net . SOA # Do we actually have v6 connectivity?
    QLA6=" ::"
else
    echo \$ dig -4 @k.root-servers.net . SOA
    dig -4 @k.root-servers.net . SOA # Do we actually have v4 connectivity?
    QLA6=""
fi

rm -f recursor.pid pdns_recursor.pid
/usr/bin/time --format '%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps
<measurement><name>user CPU seconds</name><value>%U</value></measurement>
<measurement><name>system CPU seconds</name><value>%S</value></measurement>
<measurement><name>wallclock seconds</name><value>%e</value></measurement>
<measurement><name>%% CPU used</name><value>%P</value></measurement>
'         ${RECURSOR} --daemon=no --local-port=$port --socket-dir=./ --trace=$TRACE --config-dir=. --max-mthreads=$mthreads --query-local-address="0.0.0.0${QLA6}" --threads=$threads --record-cache-shards=$shards --refresh-on-ttl-perc=10 --dnssec=validate --pdns-distributes-queries --reuseport=no --enable-old-settings > recursor.log 2>&1 &
sleep 3
if [ ! -e pdns_recursor.pid ]; then
        cat recursor.log
        echo Recursor did not start or did not write pdns_recursor.pid, exiting
        exit 1
fi


# warm up the cache
echo
echo === First run with IPv6=$IPv6 limit=$limit threads=$threads mthreads=$mthreads shards=$shards ===
${DNSBULKTEST} --www=false -qe 127.0.0.1 $port $limit < ${CSV} > bulktest.results
kill -USR1 $(cat pdns_recursor.pid) || true
${RECCONTROL} --timeout=20 --socket-dir=. --config-dir=. get-all || true

sleep 5

# rerun 1 with hot cache
echo
echo === Second run with IPv6=$IPv6 limit=$limit threads=$threads mthreads=$mthreads shards=$shards ===
${DNSBULKTEST} --www=false -qe 127.0.0.1 $port $limit < ${CSV} > bulktest.results
kill -USR1 $(cat pdns_recursor.pid) || true
${RECCONTROL} --timeout=20 --socket-dir=. --config-dir=. get-all || true

sleep 5

# rerun 2 with hot cache
echo
echo === Third run with IPv6=$IPv6 limit=$limit threads=$threads mthreads=$mthreads shards=$shards ===
${DNSBULKTEST} --www=false -qe 127.0.0.1 $port $limit < ${CSV} > bulktest.results
kill -USR1 $(cat pdns_recursor.pid) || true
${RECCONTROL} --timeout=20 --socket-dir=. --config-dir=. get-all || true

sleep 1
${RECCONTROL} --timeout=20 --socket-dir=. --config-dir=. ping
${RECCONTROL} --timeout=20 --socket-dir=. --config-dir=. quit-nicely
#
# Since quit-nicely is now synchronous, we should not need to wait for the log to be up-date-date anymore
#sleep 1
echo
echo "=== RECURSOR LOG ==="
cat recursor.log
echo "=== END RECURSOR LOG ==="

. ./bulktest.results

mkdir -p recursor-bulktest
rm -f failed_tests passed_tests
touch failed_tests passed_tests
: ${THRESHOLD:="95"}

ANANSWER=$[(100*(${DBT_QUEUED}-${DBT_ERRORS}-${DBT_TIMEOUTS}) )/${DBT_QUEUED}]

if [ $ANANSWER -ge $THRESHOLD ]
then
    echo recursor-bulktest >> passed_tests
    RETVAL=0
else
    echo "::error title=Recursor-bulktest::Bulk test failed: less than ${THRESHOLD}% of queries answered successfully"
    echo recursor-bulktest >> failed_tests
    RETVAL=1
fi

echo "$DBT_OKPERCENTAGE% of domains resolved" > recursor-bulktest/diff
egrep 'PowerDNS.COM|^<measurement' recursor.log > recursor-bulktest/stdout
echo "<measurement><name>% domains resolved</name><value>$DBT_OKPERCENTAGE</value></measurement>" >> recursor-bulktest/stdout

: ${context:="recursor-bulktest"}

export context
testsdir=. ./toxml

exit $RETVAL
